<?php
/**
 * Created by PhpStorm.
 * User: wuz
 * Date: 2016/9/26
 * Time: 12:01
 */
namespace MyApp\Controllers\Front;

use MyApp\Controllers\Front\BaseController;
use MyApp\Models\Course;
use MyApp\Models\Select;
use MyApp\Models\Student;
use Phalcon\Mvc\View;

/*
 * 选课控制器
 * */
class SelectController extends BaseController
{

    /*选课结果*/
    public function listAction()
    {
        $selcet = new Select();
        //搜索
        if ($this->request->isPost()) {
            $search = $this->request->getPost('search');
            //多字段联合模糊搜索
            $data = $selcet->select('*', [
                "OR" => ['studentName[~]' => [$search], 'courseName[~]' => [$search]]]);
            $this->view->setVar('data', $data);
        } else {
            $data = $selcet->select('*', ''); //最新十条
            $this->view->setVar('data', $data);
        }
        $this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
    }

    /*
     * 选课
     * */
    public function selectAction()
    {
        $selcet     = new Select();
        $course     = new Course();
        $courseList = $course->select('*', '');
        $data       = $selcet->select('*', '');
        $this->view->setVar('list', $courseList);
        //选课处理插入
        if ($this->request->isPost()) {
            $studentName = $this->request->getPost('studentName');
            $courseId    = $this->request->getPost('course');
            $course      = new Course();
            $student     = new Student();
            $studentId   = $student->select("id", ['studentName' => $studentName]);
            foreach ($courseId as $row) {
                $courseName = $course->select("courseName", ['id' => $row]);
                $selcet->insert([
                    'studentId' => (int) $studentId, 'studentName' => $studentName, 'courseId' => (int) $row, 'courseName' => $courseName,
                ]);
            }
        }

        $this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
    }
    public function addAction()
    {

        $student = new Student();
        $data    = $student->select('*', 'order by time desc limit 4');

    }

    public function delectAction()
    {
        $student = new Student();
        $data    = $student->select("*", '');

    }

}
